package lablayer.view;

import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.Panel;
import javafx.scene.paint.Color;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.*;
import javafx.scene.control.Label;
import lablayer.model.ComponentFactory;
import lablayer.model.MathComponents.MathComponentFactory;
import lablayer.controller.UpdaterFactory;
import lablayer.view.stat.ViewBoxBuffer;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import lablayer.view.state.StateFactory;
import lablayer.view.stat.ViewLinkComponentBuffer;
import lablayer.view.state.StateInterface;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollView;
import javafx.scene.layout.LayoutInfo;

/**
 * @author Darkness
 */

public class ViewMinValue extends ViewComponent {
    var lResult: Label;
	var inputData: Label;
    var panel: Panel;

    init {
        this.component = ComponentFactory.createComponent(MathComponentFactory.
            createMinValue());
        this.component.setUpdater(UpdaterFactory.createUpdater(this));        
    }

    override public function create(): Node {
        var vbox: VBox;
        panel = Panel {
            layoutX: 0
            layoutY: 0
            width: 250
            height: 200

            content: [
                //background
                Rectangle {
                    layoutX: 0
                    layoutY: 0
                    width: bind panel.width
                    height: bind panel.height
                    fill: Color.web("#FFFFFF")
                    arcWidth: 15
                    arcHeight: 15
                    stroke: Color.web("#59649C")
                    strokeWidth: 1
                },
                vbox = VBox {
                    var startX: Float;
                    var startY: Float;                    

                    layoutX: 5
                    layoutY: 5
                    width: bind (panel.width - 10)
                    height: bind (panel.height - 10)

                    content: [
                        Group {
                            layoutX: 0
                            layoutY: 0
                            content: [
                                //header
                                Rectangle {
                                    layoutX: 0
                                    layoutY: 0
                                    width: bind vbox.width
                                    height: 20
                                    fill: LinearGradient {
                                        startX: 0, startY: 0, endX: 225, endY: 0
                                        proportional: false
                                        stops: [
                                            Stop { offset: 0.0 color: Color.web("#5277BA") }
                                            Stop { offset: 1.0 color: Color.web("#4969A6") },
                                        ]
                                    }
                                    arcWidth: 10
                                    arcHeight: 10

                                    onMousePressed: function(e: MouseEvent): Void {
                                        if (StateFactory.getState().getStateId() == StateInterface.COMPONENT_STATE) {
											startX = this.translateX;
		                                    startY = this.translateY;
			                                this.toFront();
										}
                                    }
                                    onMouseDragged: function(e: MouseEvent): Void {
										if (StateFactory.getState().getStateId() == StateInterface.COMPONENT_STATE) {
											this.translateX = startX + e.dragX;
		                                    this.translateY = startY + e.dragY;
										}
                                    }
                                },
                                Text {
                                    layoutX: 5
                                    layoutY: 14
                                    content: this.component.getComponentName()
                                    fill: Color.web("#FFFFFF")
                                }
                            ]
                        },
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
						Label {
							text: "Входные данные"
						},
						Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
						ScrollView {
							node: inputData = Label {
								text: ""
								textFill: Color.GRAY
							}
							layoutInfo: LayoutInfo {
								height: 35
								maxHeight: 35
							}
						},
						Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
                        Label {
                            text: "Значение"
                        },
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
                        ScrollView {
							node: lResult = Label {
								text: ""
								textFill: Color.web("#252C4F")
							}
						},
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
						HBox {
                            content: [
                                Button {
                                    text: "Закрыть"
                                    onMouseClicked: function(e: MouseEvent): Void {
                                        if (StateFactory.getState().getStateId() == StateInterface.COMPONENT_STATE) {
											ViewBoxBuffer.getViewBox().removeViewComponent(this);
										}
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]

            onMouseEntered: function(e: MouseEvent): Void {
                if (StateFactory.getState().getStateId() == StateInterface.LINK_STATE) {
                    ViewLinkComponentBuffer.setViewComponent(this);
                }
            }
            onMouseExited: function(e: MouseEvent): Void {
                if (StateFactory.getState().getStateId() == StateInterface.LINK_STATE) {
                    ViewLinkComponentBuffer.clear();
                }
            }
        }
    }
	
	override public function update () : Void {
        this.lResult.text = this.component.getResult().toString();
		this.inputData.text = this.component.getData().toString();
    }

}
